Typical HAL SRAM use cases for migration ¶
This section provides information about typical HAL SRAM use cases for migration.
Note
HAL2, HAL SRAM do only the FMC configuration to access the external memory, No more processing (Read/Write) APIs anymore. Once the FMC is configured, SRAM become memory mapping and accessible for CPU and other master. To checkout the different cases of configuration and how to use them see HAL FMC SRAM initialization and configuration functions migration.
Use case: Read and write operations ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* - */
// Write data to the SRAM memory
HAL_SRAM_Write_8b(&hsram,
(uint32_t *)(target_address),
data_to_write,
BufferSize
);
// Read data from the SRAM memory
HAL_SRAM_Read_8b(&hsram,
(uint32_t *)(target_address),
data_read,
BufferSize
);
|
uint32_t size;
uint8_t *psramaddress = (uint8_t *)target_address;
uint8_t *psrcbuff = data_to_write;
uint8_t *pdestbuff = data_read;
/* Write data to memory */
for (size = BufferSize; size != 0U; size--)
{
*psramaddress = *psrcbuff;
psrcbuff++;
psramaddress++;
}
/* Read data from memory */
for (size = BufferSize; size != 0U; size--)
{
*pdestbuff = *psramaddress;
pdestbuff++;
psramaddress++;
}
|
Key changes ¶
In HAL2, the SRAM driver only configures FMC; there are no dedicated SRAM read/write APIs.
Once FMC is configured, SRAM is memory-mapped and accessed by direct CPU reads/writes.
Use case: Read and write DMA operations ¶
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Write data to the SRAM memory -------------------- */
HAL_SRAM_Write_DMA(&hsram,
(uint32_t *)(target_address),
data_to_write,
BufferSize
);
/* Read data from the SRAM memory */
HAL_SRAM_Read_DMA(&hsram,
(uint32_t *)(target_address),
data_read,
BufferSize
);
|
/* Write data to memory */
HAL_DMA_StartDirectXfer_IT(&dma_handle,
data_to_write,
(uint32_t *)(target_address),
BufferSize
);
/* Read data from memory */
HAL_DMA_StartDirectXfer_IT(&dma_handle,
(uint32_t *)(target_address),
data_read,
BufferSize
);
|
Key changes ¶
In HAL2, SRAM read/write is done through memory mapping; DMA transfers use direct DMA APIs.
HAL1 uses
HAL_SRAM_Read_DMA()/HAL_SRAM_Write_DMA()while HAL2 usesHAL_DMA_StartDirectXfer_IT().
Use case: Disable unused memory ¶
Note
When external SRAM is not needed, it is possible to disable HAL SRAM to save power.
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
|
The only option to disable SRAM is to deinitialize the HAL SRAM driver. When SRAM is needed again, the initialization sequence must be performed again. |
It is possible to disable HAL SRAM without full deinitialization when SRAM is no longer needed, and to re-enable it when the external memory is required again. |
|
|
|
/* Perform any access operation */
/* Deinitialize the SRAM */
HAL_SRAM_DeInit(&hsram);
/* Reinitialize the SRAM */
HAL_SRAM_Init(&hsram, &Sram_Timing, NULL);
/* Perform any access operation */
|
/* Perform any access operation */
/* Disable an SRAM sub-bank to save power */
HAL_SRAM_DisableMemory(&hsram);
/* Enable the corresponding SRAM sub-bank */
HAL_SRAM_EnableMemory(&hsram);
/* Perform any access operation */
|
Key changes ¶
In HAL2, SRAM can be disabled/enabled without full deinitialization.
In HAL1, disabling SRAM requires deinitializing and reinitializing the driver.
Use case: Disable and enable write protection ¶
Note
The HAL1 API name for disabling write operations has been renamed to write protection enable in HAL2.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
/* Enable write operation */
HAL_SRAM_WriteOperation_Enable(&hsram);
/* Do some write operations if needed */
/* Disable write operation */
HAL_SRAM_WriteOperation_Disable(&hsram);
|
/* Disable the Write protection */
HAL_SRAM_DisableWriteProtection(&hsram);
/* Do some write operations if needed */
/* Enable the Write protection */
HAL_SRAM_EnableWriteProtection(&hsram);
|
Key changes ¶
HAL2 uses explicit write protection APIs (enable/disable write protection).
HAL1 enables/disables write operations using
HAL_SRAM_WriteOperation_Enable()/HAL_SRAM_WriteOperation_Disable().
Related examples in HAL2 ¶
|
Example name |
Brief |
|---|---|
|
example_hal_fmc_sram_write_protection |
Enabling FMC SRAM write protection and triggering a hard fault in case of an erroneous write access. |







